package de.chrisbremer.hydromap;

/**
 * @author 	chris
 * @date   	2011-12-23
 */

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;
import de.chrisbremer.hydromap.R;

public class Alerts {
public static void ShowEmpAddedAlert(Context con)
{
	AlertDialog.Builder builder=new AlertDialog.Builder(con);
	builder.setTitle(con.getText(R.string.alerts_title_newHydrant));
	builder.setIcon(android.R.drawable.ic_dialog_info);
	DialogListner listner=new DialogListner();
	builder.setMessage(con.getText(R.string.alerts_title_newHydrantSuccessful));
	builder.setPositiveButton(con.getText(R.string.alerts_title_ok), listner);
	
	AlertDialog diag=builder.create();
	diag.show();
}

public static AlertDialog ShowEditDialog(final Context con,final Hydrant hydr)
{
	AlertDialog.Builder b=new AlertDialog.Builder(con);
	b.setTitle(con.getText(R.string.alerts_title_detail));
	LayoutInflater li=LayoutInflater.from(con);
	View v=li.inflate(R.layout.editdialog, null);
	
	b.setIcon(android.R.drawable.ic_input_get);
	
	b.setView(v);
	final Spinner spinType=(Spinner)v.findViewById(R.id.spinDiagType);
	final TextView txtDiameter=(TextView)v.findViewById(R.id.txtDelDiameter);
	final Spinner spinPosition=(Spinner)v.findViewById(R.id.spinDiagPos);
	final TextView txtGPSLat=(TextView)v.findViewById(R.id.txtDelGPSLat);
	final TextView txtGPSLon=(TextView)v.findViewById(R.id.txtDelGPSLon);
	final TextView txtCount=(TextView)v.findViewById(R.id.txtDelCount);
	final TextView txtCapacity=(TextView)v.findViewById(R.id.txtDelCapacity);
	final TextView txtComment=(TextView)v.findViewById(R.id.txtDelComment);
	
	
	Utilities.ManageTypeSpinner(con, spinType);
	for(int i=0;i<spinType.getCount();i++)
	{
		long id=spinType.getItemIdAtPosition(i);
		if(id==hydr.getType())
		{
			spinType.setSelection(i, true);
			break;
		}
	}
	
	Utilities.ManagePositionSpinner(con, spinPosition);
	for(int i=0;i<spinPosition.getCount();i++)
	{
		long id=spinPosition.getItemIdAtPosition(i);
		if(id==hydr.getPosition())
		{
			spinPosition.setSelection(i, true);
			break;
		}
	}
	txtDiameter.setText(Integer.valueOf(hydr.getDiameter()).toString());
	txtGPSLat.setText(hydr.getGPSLat());
	txtGPSLon.setText(hydr.getGPSLon());
	txtCount.setText(hydr.getCount());
	txtCapacity.setText(Float.valueOf(hydr.getCapacity()).toString());
	txtComment.setText(hydr.getComment());
	
	//UPDATE
	b.setPositiveButton(con.getText(R.string.alerts_title_change), new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			hydr.SetType((int)spinType.getItemIdAtPosition(spinType.getSelectedItemPosition()));
			hydr.setDiameter(Integer.valueOf(txtDiameter.getText().toString()));
			hydr.setPosition((int)spinPosition.getItemIdAtPosition(spinPosition.getSelectedItemPosition()));
			hydr.setGPSLat(txtGPSLat.getText().toString());
			hydr.SetGPSLon(txtGPSLon.getText().toString());
			hydr.setCount(txtCount.getText().toString());
			hydr.setCapacity(Float.valueOf(txtCapacity.getText().toString()));
			hydr.setComment(txtComment.getText().toString());
			hydr.setSend(true);
			hydr.setLastUpdate();
			
			try
			{
			DatabaseHelper db=new DatabaseHelper(con);
			db.updateHydrant(hydr);
			db.close();
			}
			catch(Exception ex)
			{
				CatchError(con, ex.toString());
			}
		}
	});
	
	//DELETE
	b.setNeutralButton(con.getText(R.string.alerts_title_delete), new OnClickListener() {
		
		public void onClick(DialogInterface dialog, int which) {
			DatabaseHelper db=new DatabaseHelper(con);
			db.deleteHydrant(hydr.getID());
			db.close();
		}
	});
	
	//CANCEL
	b.setNegativeButton(con.getText(R.string.alerts_title_cancel), null);
	
	return b.create();
	//diag.show();
	
}

static public void CatchError(Context con, String Exception)
{
	Dialog diag=new Dialog(con);
	diag.setTitle("Fehler");
	TextView txt=new TextView(con);
	txt.setText(Exception);
	diag.setContentView(txt);
	diag.show();
}


}


